 package com.zu.commu.auto1_0.protocol.call;
 
 import com.zu.commu.auto1_0.DataType1_0;
import com.zu.commu.auto1_0.protocol.CheckData;
import com.zu.commu.auto1_0.protocol.Util;
import com.zu.commu.exception.CommuException;
import com.zu.commu.interf.Data;
import com.zu.commu.util.DateTime;
 
 public class ReportDataProtocol
 {
   public Data analyseReportData(byte[] b)
     throws Exception
   {
     new CheckData().checkCrc(b, 4, 27, 28);
     Data d = null;
     d = new Data();
     getMeterNo(b, d);
     getStatus(b, d);
     //剩余水量-电池电压7
     getRemainWater(b, d);
     //累计取水量-信号强度
     getAccumulateFlux(b, d);
     //瞬时流量-探头埋深
     getInstantFlux(b, d);
     //充值时间-电流
     getChargeTimes(b, d);
     //水位
     getWaterLevel(b, d);
 
     d.setDateTime(DateTime.yymmddhhmmss());
     if(b[11]==-50){
    	 d.setDataType(DataType1_0.SETCLOCK.toInt()+"");
    	 return d;
     }
     d.setDataType(DataType1_0.REPORTDATA.toInt()+"");
     return d;
   }
 
   private void getMeterNo(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       d.setMeterNo(u.byte2Hex(b, 4, 9));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取测控器地址号时出错！" + e.getMessage(), null);
     }
   }
 
   private void getStatus(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       String s = u.byte2Binary(b[12]);
       while (s.length() < 8) {
         s = "0" + s;
       }
       d.setSensorStatus(s.charAt(0)+"");
       d.setPowerStatus(s.charAt(1)+"");
       d.setPumpStatus(s.charAt(2)+"");
       d.setDoorStatus(s.charAt(3)+"");
       d.setRemainWaterStatus(s.charAt(4)+"");
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取各种状态时出错！" + e.getMessage(), null);
     }
   }
 
   private void getRemainWater(byte[] b, Data d)
     throws Exception
   {
     Util u = new Util();
     String minus = "";
     try {
       String s = u.byte2Binary(b[13]);
       if (s.length() > 4) {
         minus = "-";
         byte[] tmp38_35 = b; tmp38_35[13] = (byte)(tmp38_35[13] - 16);
       }
       d.setRemainWater(Double.valueOf(u.string2Double(minus + u.clean0FromStrNum(u.byte2Hex(b, 13, 16)))));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取剩余水量时出错！" + e.getMessage(), null);
     }
   }
 
   private void getAccumulateFlux(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       double dd = u.string2Double(u.clean0FromStrNum(u.byte2Hex(b, 17, 20)));
       if (dd > 10000000.0D) {
         dd = 10000000.0D - dd;
       }
       d.setAccumulateFlux(Double.valueOf(dd));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取累积用水量时出错！" + e.getMessage(), null);
     }
   }
 
   private void getInstantFlux(byte[] b, Data d)
     throws Exception
   {
     Util u = new Util();
     String minus = "";
		try {
			String s = u.byte2Binary(b[21]);
			if (s.length() > 4) {
				minus = "-";
				byte[] tmp38_35 = b;
				tmp38_35[21] = (byte) (tmp38_35[21] - 16);
			}
			Double dd =u.string2Double(minus+ u.clean0FromStrNum(u.byte2Hex(b, 21, 22)));
			d.setInstantFlux(Double.valueOf(dd/100));
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取剩余水量时出错！" + e.getMessage(), null);
		}
   }
 
   private void getChargeTimes(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       d.setChargeTimes(Integer.valueOf(u.string2Int(u.clean0FromStrNum(u.byte2Hex(b, 23, 24)))));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取充值次数时出错！" + e.getMessage(), null);
     }
   }
 
   private void getWaterLevel(byte[] b, Data d)
     throws Exception
   {
     try
     {
       Util u = new Util();
       String hight = u.clean0FromStrNum(u.byte2Hex(b, 25, 26));
       String low = u.clean0FromStrNum(u.byte2Hex(b, 27, 27));
       d.setWaterLevel(Double.valueOf(u.string2Double(hight + "." + low)));
     } catch (Exception e) {
       throw new CommuException().setMessage("从上报数据中读取水位时出错！" + e.getMessage(), null);
     }
   }
 }
